package crypto.process;

import crypto.reference.References;

import crypto.util.RandomCrypto;

/**
 * DOCUMENT ME!
 *
 * @author gato
 */
public class Sustitution {
  private int addChar;
  private int variation = 0;

  /**
   * Metodo que desplaza el valor del caracter en X valores dependiendo del char generado al
   * azar.
   *
   * @param text [String]
   *
   * @return texto modificado [String]
   */
  public String addCharacter(String text) {
    boolean sequence  = true;
    char[]  arrayText = text.toCharArray();

    //Genero int equivalente a una letra del alfabeto
    addChar = RandomCrypto.getRandomInt(26) + 1;

    int addCharCopy = this.addChar;
    variation = RandomCrypto.getRandomInt(10) + 1;

    //Le "sumo" esta letra a todas las letras del array
    for (int i = 0; i < arrayText.length; i++) {
      if (sequence) {
        addChar = getAddChar() - getVariation();
        arrayText[i] += getAddChar();
        sequence = false;
      } else {
        addChar = getAddChar() + getVariation();
        arrayText[i] += getAddChar();
        sequence = true;
      }
    }

    this.addChar = addCharCopy;

    return new String(arrayText).trim();
  }

  /**
   * DOCUMENT ME!
   *
   * @param text DOCUMENT ME!
   * @param reference DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public String substractCharacter(String text, References reference) {
    boolean sequence  = true;
    char[]  arrayText = text.toCharArray();

    //Genero int equivalente a una letra del alfabeto
    addChar     = reference.getSustitutionChar();
    variation   = reference.getSustitutionVariation();

    //Le "resto" esta letra a todas las letras del array
    for (int i = 0; i < arrayText.length; i++) {
      if (sequence) {
        addChar = addChar - getVariation();
        arrayText[i] -= getAddChar();
        sequence = false;
      } else {
        addChar = addChar + getVariation();
        arrayText[i] -= getAddChar();
        sequence = true;
      }
    }

    return new String(arrayText).trim();
  }

  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public int getAddChar() {
    return this.addChar;
  }

  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public int getVariation() {
    return this.variation;
  }
}
